package com.Xht.www.serviceImpl;
//实现处理用户事务的逻辑

import com.Xht.www.dao.UserInfoDAO;
import com.Xht.www.dao.UserCheckinDAO;
import com.Xht.www.dao.UserPointsDAO;
import com.Xht.www.po.UserInfoPO;
import com.Xht.www.po.UserCheckinPO;
import com.Xht.www.po.UserPointsPO;
import com.Xht.www.service.User;

import java.sql.Timestamp;
import java.util.Calendar;

public class UserImpl implements User {

    private final UserInfoDAO userInfoDAO = new UserInfoDAO();
    private final UserCheckinDAO userCheckinDAO = new UserCheckinDAO();
    private final UserPointsDAO userPointsDAO = new UserPointsDAO();


    @Override
    public UserInfoPO getUserByUsername(String username) {
        return userInfoDAO.getUserByUsername(username);
    }

    @Override
    public boolean registerUser(UserInfoPO user) {
        return userInfoDAO.insertUser(user);
    }

    @Override
    public boolean updateUser(UserInfoPO user) {
        return userInfoDAO.updateUser(user);
    }

    @Override
    public boolean deleteUser(int userId) {
        return userInfoDAO.deleteUser(userId);
    }

    @Override
    public UserInfoPO getUserById(int userId) {
        return userInfoDAO.getUserById(userId);
    }

    @Override
    public boolean userCheckin(UserCheckinPO checkinPO) {
        int userId = checkinPO.getUserId();
        UserInfoPO user = userInfoDAO.getUserById(userId);
        if (user == null) {
            return false; // 用户不存在
        }

        if (userCheckinDAO.hasCheckedInWithin24Hours(userId)) {
            return false; // 用户在24小时内已经签到过
        }

        boolean checkinResult = userCheckinDAO.addUserCheckin(checkinPO);
        if (checkinResult) {
            UserPointsPO userPoints = userPointsDAO.getUserPointsById(userId);
            if (userPoints == null) {
                userPoints = new UserPointsPO();
                userPoints.setUserId(userId);
                userPoints.setTotalPoints(0);
                userPoints.setConsecutiveDays(0);
                userPoints.setCumulativeDays(0);
            }

            // 获取上一次签到时间
            Timestamp lastCheckinTime = userCheckinDAO.getLastCheckinTime(userId);
            if (lastCheckinTime != null) {
                Calendar lastCheckinCalendar = Calendar.getInstance();
                lastCheckinCalendar.setTime(lastCheckinTime);

                Calendar nowCalendar = Calendar.getInstance();
                nowCalendar.add(Calendar.HOUR_OF_DAY, -24);

                if (lastCheckinCalendar.before(nowCalendar)) {
                    // 连续签到中断，将连续签到天数重置为 0
                    userPoints.setConsecutiveDays(0);
                }
            }

            int pointsEarned = checkinPO.getPointsEarned();
            userPoints.setTotalPoints(userPoints.getTotalPoints() + pointsEarned);
            userPoints.setConsecutiveDays(userPoints.getConsecutiveDays() + 1);
            userPoints.setCumulativeDays(userPoints.getCumulativeDays() + 1);

            if (userPoints.getPointsId() == null) {
                return userPointsDAO.insertUserPoints(userPoints);
            } else {
                return userPointsDAO.updateUserPoints(userPoints);
            }
        }
        return false;
    }
}

